package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmAuthDto;
import com.heima.model.wemedia.pojos.UserAudit;
import com.heima.wemedia.mapper.WmUserAuditMapper;
import com.heima.wemedia.service.WmUserAuditService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class WmUserAuditServiceImpl extends ServiceImpl<WmUserAuditMapper, UserAudit> implements WmUserAuditService {
    @Autowired
    private WmUserAuditMapper wmUserAuditMapper;
    @Override
    public ResponseResult findAll(WmAuthDto dto) {
        if (dto==null){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        IPage page=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<UserAudit> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAudit::getState,dto.getStatus());
        page=page(page,lambdaQueryWrapper);
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult failAndpass(WmAuthDto dto,Short status) {
        if (dto==null){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        UserAudit userAudit=new UserAudit();
        userAudit.setId(dto.getId());
        userAudit.setState(status);
        if (StringUtils.isNotBlank(dto.getMsg())){
            userAudit.setReason(dto.getMsg());
        }
        if (status.equals("9")){
            registeredUser(dto);
        }
        return null;
    }

    private void registeredUser(WmAuthDto dto) {
        Integer dtoId = dto.getId();
        UserAudit userAudit = getById(dtoId);
        if (userAudit==null){
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Integer userId = userAudit.getUserId();
        UserAudit userAudit1 = wmUserAuditMapper.selectById(userId);
    }
}
